'use strict';

/**
 * @ngdoc directive
 * @name jgappApp.directive:userDefined
 * @description
 * # userDefined
 */
angular.module('jgappApp')
    .directive('userDefined', function (Util, openApi, DataService, $routeParams) {
        return {
            scope: {
                fieldMap: "=fieldMap",
                validation: "=validation",
            },
            template: '<div class="item-userDefined mt10" ng-show="show">' +
            '<p class="list-label c-blue">自定义字段</p>' +
            '<div class="list-item flex" ng-repeat="(key,item) in formData" ng-class={"required":item.allowBlank==0,"mt0":$first}>' +
            '<div class="w35 c9">' +
            '{{item.fieldLabel}}:' +
            '</div>' +
            '<div class="w65" ng-switch="item.fieldType">' +
            '<input type="text" ng-model="item.value" ng-change="changeHandle()" placeholder="{{item.defaultValue}}" ng-switch-when="1"/>' +
            '<select ng-model="item.value" ng-change="changeHandle()" ng-options="item as item for item in item.listValues|arrfilter" ng-switch-when="2" ng-disabled="checkView">' +
            '<option value="" ng-model="item.defaultValue">{{item.defaultValue||"请选择"}}</option>' +
            '</select>' +
            '<input type="text" ng-model="item.value" ng-change="changeHandle()" placeholder="{{item.defaultValue}}" tel-directive  ng-switch-when="3"/>' +
            '<label class="icon_clock" ng-switch-when="4">'+
            '<input type="text" ng-model="item.value" ng-change="changeHandle()" placeholder="{{item.defaultValue}}" date-directive/>' +
            '</label>'+
            '<label  class="icon_clock" ng-switch-when="5">'+
            '<input type="text" ng-model="item.value" ng-change="changeHandle()" placeholder="{{item.defaultValue}}" date-directive datetype="datetime"/>' +
            '</label>'+
            '</div>' +
            '</div>' +
            '</div>',
            restrict: 'AE',
            replace: true,
            link: function postLink(scope, element, attrs) {
                // 数据要从接口中拿到，不然由于异步原因无法拿到fieldMap值
                scope.formData = {};
                // 如果是审批页面只做展示
                var fieldMap = scope.fieldMap || {}
                var init = function () {
                    updateOrNewForm()
                }
                scope.changeHandle = function () {
                    megFieldMap()
                    // 获取验证信息
                    scope.validation = getValidation()
                    scope.fieldMap = fieldMap
                }
                var getValidation = function () {
                    var validation = {
                        passvalidation: true,
                        tip: ""
                    }
                    for (var i in scope.formData) {
                        if (scope.formData[i].allowBlank == 0) {
                            if (!scope.formData[i].value && !scope.formData[i].defaultValue) {
                                validation.passvalidation = false;
                                validation.tip = "请输入" + scope.formData[i].fieldLabel
                                break;
                            }
                        }
                    }
                    return validation
                }
                var megFieldMap = function () {
                    for (var i in scope.formData) {
                        fieldMap[i] = scope.formData[i].value || scope.formData[i].defaultValue
                    }
                }
                var updateOrNewForm = function () {
                    var param = {};
                    param.formKey = Util.getIconUrl();
                    var TVtype;
                    // 如果是驳回的则带上参数
                    TVtype = Util.getTVType();
                    if (TVtype == Util.UPDATE) {
                        param.taskId = $routeParams.id
                    }
                    DataService.getData(openApi.customFields, param).then(function (data) {
                        // 显示数据
                        if (data) {
                            scope.show = true
                        }
                        scope.formData = data;
                        for (var i in scope.fieldMap) {
                            scope.formData[i].value = scope.fieldMap[i]
                        }
                        scope.changeHandle()
                    })
                };
                init()
            }
        };
    });
